package com.gzist.elderlyserviceplatform.controller.common;

import com.gzist.elderlyserviceplatform.common.Result;
import com.gzist.elderlyserviceplatform.entity.dto.ChunkUploadDTO;
import com.gzist.elderlyserviceplatform.service.MinioFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 文件云管理模块
 */
@RestController
@Tag(name = "文件存储服务模块")
@RequestMapping("/api/common/file")
@RequiredArgsConstructor
public class MinioFileController {

    private final MinioFileService minioFileService;

    @Operation(summary = "文件上传", description = "支持多种文件类型上传")
    @PostMapping("/upload")
    public Result<String> uploadFile(
            @Parameter(description = "待上传的文件", required = true)
            @RequestBody MultipartFile file
    ) {
        return Result.success(minioFileService.uploadFile(file), "文件上传成功!");
    }

    @Operation(summary = "文件流上传", description = "通过文件流上传文件")
    @PostMapping("/upload/stream")
    public Result<String> uploadFileByStream(
            @Parameter(description = "文件流数据", required = true) InputStream inputStream,
            @Parameter(description = "文件类型", required = true) @RequestParam("fileType") String fileType,
            @Parameter(description = "文件名", required = true) @RequestParam("fileName") String fileName
    ) {
        return Result.success(minioFileService.uploadFileByStream(inputStream, fileType, fileName), "文件上传成功!");
    }

    @Operation(summary = "Base64图片上传", description = "支持Base64编码的图片上传")
    @PostMapping("/upload/image")
    public Result<String> uploadImage(
            @Parameter(description = "Base64编码的图片", required = true) @RequestParam("imageBase64") String imageBase64
    ) {
        return Result.success(minioFileService.uploadImage(imageBase64), "图片上传成功!");
    }

    @Operation(summary = "文件删除", description = "通过文件ID删除文件")
    @DeleteMapping("/delete/{id}")
    public Result deleteFile(
            @Parameter(description = "文件ID", required = true) @PathVariable("id") String id
    ) {
        minioFileService.deleteFile(id);
        return Result.success("文件删除成功!");
    }

    @Operation(summary = "批量删除文件", description = "通过多个文件ID批量删除文件")
    @DeleteMapping("/batchDelete")
    public Result<String> deleteFiles(
            @Parameter(description = "文件ID列表", required = true) @RequestParam("ids") List<String> ids
    ) {
        minioFileService.deleteFiles(ids);
        return Result.success("文件批量删除成功!");
    }

    @Operation(summary = "获取文件外链", description = "通过文件ID获取文件的访问外链")
    @GetMapping("/getFileUrl/{id}")
    public Result<String> getFileUrl(
            @Parameter(description = "文件ID", required = true) @PathVariable("id") String id
    ) {
        return Result.success(minioFileService.getFileUrl(id), "文件外链获取成功!");
    }

    @Operation(summary = "检查分片状态")
    @GetMapping("/chunk/status")
    public Result<Map<String, Object>> checkChunkStatus(
            @Parameter(description = "文件MD5", required = true) @RequestParam String fileMd5,
            @Parameter(description = "总分片数", required = true) @RequestParam Integer totalChunks
    ) {
        return Result.success(minioFileService.checkChunkStatus(fileMd5, totalChunks), "状态检查成功");
    }

    @Operation(summary = "智能分片上传", description = "文件与元数据合并上传接口")
    @Parameters({
            @Parameter(name = "chunk", description = "文件分片数据", required = true, schema = @Schema(type = "string", format = "binary")),
            @Parameter(name = "params", description = "分片元数据", required = true)
    })
    @PostMapping(value = "/smartUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> smartUpload(
            @Parameter(hidden = true) // 使用@RequestPart时需配合@Parameter
            @RequestPart("chunk") @NotNull MultipartFile chunk,
            @RequestPart("params") @Valid ChunkUploadDTO params
    ) {
        return Result.success(
                minioFileService.uploadChunkAndMerge(chunk, params),
                "分片处理成功"
        );
    }
}